package com.beamfield.common;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author Zephyr
 * @Description:
 * @Date 2018/10/9
 */
public class TokenManage {
    private static Map<String, LogInfo> tokenMap = new HashMap();

    static {
        LogInfo logInfo = new LogInfo();
        logInfo.setUserId(1L);
        logInfo.setBeamId(1L);
        logInfo.setBeamName("桐城南");
        logInfo.setRealName("miss");
        logInfo.setLoginName("admin");
        put("1", logInfo);
    }

    public TokenManage() { }

    public static String createToken(){
        return UUID.randomUUID().toString();
    }
    public static String createToken(LogInfo logInfo) {
        String token = createToken();
        put(token, logInfo);
        return token;
    }

    public static boolean isRight(String token) {
        return containsKey(token);
    }

    public static boolean removeToken(String token) {
        LogInfo logInfo = get(token);
        if (logInfo == null) {
            return true;
        } else {
            remove(token);
            return true;
        }
    }

    public static void put(String token, LogInfo logInfo) {
        tokenMap.put(token, logInfo);
    }

    private static void remove(String token) {
        tokenMap.remove(token);
    }

    public static LogInfo get(String token) {
        return (LogInfo)tokenMap.get(token);
    }

    private static boolean containsKey(String token) {
        return tokenMap.containsKey(token);
    }
}
